package com.wujian.love.controller;

import com.wujian.love.core.token.FormToken;
import com.wujian.love.dto.OrderInfoDto;
import com.wujian.love.service.IOrderParentService;
import com.wujian.love.result.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName: OrderController
 * @description:
 * @author: wujian
 * @Date: 2020-06-12 15:43
 **/
@RestController
@RequestMapping("/love/order")
public class OrderController {

    @Autowired
    private IOrderParentService orderParentService;

    @GetMapping("/getOrderInfo")
    @FormToken(save = true)
    public JSONResult getOrderInfo(HttpServletRequest request){
        System.out.println("展示订单商品信息页面");
        String formToken = (String) request.getSession().getAttribute("formToken");
        System.out.println("formToken"+formToken);
        return JSONResult.ok(formToken);
    }


    /**
     * @Author WuJian
     * @Description //创建订单
     * @Date 15:50 2020/6/12
     * @Param []
     * @return com.wujian.love.result.JSONResult
     **/
    @PostMapping("/createOrder")
    @FormToken(remove = true, pass = true)
    public  JSONResult createOrder(@Validated @RequestBody OrderInfoDto dto, BindingResult bindingResult, HttpServletRequest request){
        try {
            if(bindingResult.hasErrors()){
                return JSONResult.errorMsg(bindingResult.getFieldError().getDefaultMessage());
            }
            String str = "";
            //判断是否为重复提交，如果是直接跳转
            String passRepeat = (String) request.getAttribute("formToken_pass_repeat");
        /*if ("true".equals(passRepeat)) {
           return JSONResult.errorMsg(JSONResult.SUBMIT_ERROR);
        }*/
            String clinetToken= request.getParameter("formToken");
            //重新生成一个uuid，预约失败的时候生成新的uuid
            String uuid = clinetToken;
            Map<String,Object> resultMap = orderParentService.createOrderParent(dto);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONResult.ok();
    }

}
